#include "Light.h"
s_light             Lights[16][3][4];
uint8_t             Base_Station_id[3]={0x00,0x30,0x10};
extern uint8_t      s1_send_buffer_free;
//********************************************************
// 灯列表初始化
// 两条链表，灯链表和空闲链表
//********************************************************
void light_init(void)
{
   memset(Lights,0,LIGHT_MAX*sizeof(s_light));
}

//********************************************************
// 添加灯
// 返回：0 已存在的灯    1 新灯或不符合规则的灯
//********************************************************
uint8_t add_light(s_light* light)
{
   s_light *light_in_list;
   uint8_t crow_num = light->crowd_num;
   uint8_t group_num= light->group_num;
   uint8_t tag_num  = light->Tag_num & 0x7F;
   uint8_t map_pos  = 0;

   //验证灯群组位号是否符合规则16*3*4
   if((crow_num>16)||(group_num>3)||(tag_num>4))
   {
      return 1;
   }

   //将灯添加到列表
   light_in_list = (s_light *)(&(Lights[crow_num-1][group_num-1][tag_num-1]));
   printf("%s:%d mode %d",__FILE__,__LINE__,light->mode);
   if(0 == memcmp(light_in_list->light_address,light->light_address,3))
   {
      memcpy(light_in_list->light_address,light->light_address,17);
      if(light_in_list->heart_cnt<=200)
      {
         light_in_list->heart_cnt++;
      }
      return 0;
   }
   else
   {
      memcpy(light_in_list->light_address,light->light_address,17);
      light_in_list->heart_cnt = 1;
      return 1;
   }
}

//--------------------------------------------------------
// upload_light()
//   功能： 上传缓存灯信息（心跳计数大于0）
//   输入： None
//   输出： None
//--------------------------------------------------------
void  upload_light(uint8_t *network_buffer)
{
   uint8_t      light_cnt=0;
   uint16_t     cur_pos=8;
   uint16_t     crc=0;
   uint16_t     dataCnt=4;
   s_light*     light =NULL;

   light = Lights[0][0];


   network_buffer[0] = 0x55;
   network_buffer[1] = 0xAA;

   network_buffer[4] = 0x17;
   network_buffer[5] = Base_Station_id[0];
   network_buffer[6] = Base_Station_id[1];
   network_buffer[7] = Base_Station_id[2];

   for(light_cnt=0; light_cnt<LIGHT_MAX; light_cnt++)
   {
      if(light->heart_cnt>0)
      {
            dataCnt+=17;
            memcpy(network_buffer+cur_pos,light->light_address,17);
            cur_pos+=17;
            light->heart_cnt = 0;
      }
      light++;
   }
   network_buffer[2] = dataCnt>>8;
   network_buffer[3] = dataCnt;
   crc = gen_crc(network_buffer+4,dataCnt);
   network_buffer[cur_pos++] = crc>>8;
   network_buffer[cur_pos++] = crc&0xFF;
   network_buffer[cur_pos++] = 0xDD;
 
   ch395_write_send_buf_sn(1, cur_pos, network_buffer);
   s1_send_buffer_free = 0;
}

s_light* need_to_sync(s_light *light)
{
   s_light *lighttmp;

   //
   //核实群组位编号是否超限，超限返回0
   //
   if((light->crowd_num>16)||(light->group_num>3)||(light->Tag_num>4))
   {
      return NULL;
   }
   //
   //核实灯是否是组长,不是组长灯，返回0
   //
   if(0x01 != light->Tag_num)
   {
      return NULL;
   }
   
   printf("%s:%d sync mod %d\r\n",__FILE__,__LINE__,light->mode);
   //
   //核实组员是否存在且模式一致
   //
   lighttmp = &(Lights[light->crowd_num-1][light->group_num-1][1]);
   printf("tag2 mode %d cnt %d :\r\n",lighttmp->mode,lighttmp->heart_cnt );
   if((lighttmp->heart_cnt>0)&&(lighttmp->mode!=light->mode))
   {
      return lighttmp;
   }
   
   lighttmp = &(Lights[light->crowd_num-1][light->group_num-1][2]);
   printf("tag3 mode %d cnt %d :\r\n",lighttmp->mode,lighttmp->heart_cnt );
   if((lighttmp->heart_cnt>0)&&(lighttmp->mode!=light->mode))
   {
      return lighttmp;
   }

   lighttmp = &(Lights[light->crowd_num-1][light->group_num-1][3]);
   printf("tag4 mode %d cnt %d :\r\n",lighttmp->mode,lighttmp->heart_cnt );
   if((lighttmp->heart_cnt>0)&&(lighttmp->mode!=light->mode))
   {
      return lighttmp;
   }
   printf("false\r\n");
   return NULL;
}
